from rest_framework import serializers
from Photography.models import Album, Picture, User
from django.contrib.auth.hashers import make_password


# 相册序列化
class AlbumSerializer(serializers.ModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username')

    class Meta:
        model = Album
        fields = "__all__"


# user序列化
# 校验用户数据必传项required=True
class UserSerializer(serializers.ModelSerializer):
    """
    username:用户名，必传参数
    fullname：全名，必传参数
    password：密码，必传参数
    email：邮箱，必传_数据库设置的必填
    phone：电话，必传_数据库设置的必填
    """
    id = serializers.IntegerField(read_only=True)
    username = serializers.CharField(max_length=30, min_length=4, required=True, write_only=True,
                                     error_messages={'max_length': '长度最多为30！', 'min_length': '长度最小为4！'})
    fullname = serializers.CharField(max_length=30, min_length=2, required=True, write_only=True,
                                     error_messages={'max_length': '长度最多为30！', 'min_length': '长度最小为2！'})
    password = serializers.CharField(max_length=16, min_length=4, required=True, write_only=True,
                                     error_messages={'max_length': '长度最多为16！', 'min_length': '长度最小为4！'})
    email = serializers.EmailField(max_length=30, default="", write_only=True,
                                   error_messages={'max_length': '长度最多为30！'})
    phone = serializers.CharField(max_length=11, min_length=11, write_only=True,
                                  error_messages={'max_value': '电话位数为11位！', 'min_value': '电话位数为11位！'})
    is_active = serializers.BooleanField(default=True, write_only=True)

    def create(self, validated_data):
        validated_data['password'] = make_password(validated_data.get('password'))
        return User.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.username = validated_data.get('username', instance.username)
        instance.fullname = validated_data.get('fullname', instance.fullname)
        instance.password = validated_data.get('password', instance.password)
        instance.email = validated_data.get('email', instance.email)
        instance.phone = validated_data.get('phone', instance.phone)
        instance.is_active = validated_data.get('is_active', instance.is_active)
        instance.save()
        return instance

    # 数据校验
    def validate_phone(self, value):
        from rest_framework import exceptions
        phone_number = ['133', '149', '153', '173', '177', '180', '181', '189', '199',
                        '130', '131', '132', '145', '155', '156', '166', '171', '175', '176', '185', '186', '166']
        if value[:3] not in phone_number:
            raise exceptions.ValidationError('电话号码格式错误！暂只支持电信和联通号码！')
        return value

    class Meta:
        model = User
        # 序列化全部字段
        fields = "__all__"
        # 序列化部分
        # fields = ('id', 'username', 'fullname', 'email', 'phone', 'is_active')


class PictureSerializer(serializers.ModelSerializer):
    status = serializers.BooleanField(default=1)

    class Meta:
        model = Picture
        # fields = ('picture_url', 'album_id')
        # 序列化全部
        fields = "__all__"
